<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

//资产表
class Assets extends Admin
{
    public function index(){
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        $name = $this->request->controller() . $this->request->action();
        if($this->request->isPost())
        {
            $post = input('post.');
            $where = [];
            //订单编号
            $where['orderid'] = $post['orderid']??'';
            //开始结束时间
            $where['time'] = $post['time']??'';
            //状态
            $where['class'] = $post['class']??'';
            $wheres =[];
            if($where['time']){
                list($first,$last) = explode('至',$where['time']);
                $wheres[] = ['create_time','>',trim($first)];
                $wheres[] = ['overtime','<',trim($last)];
            }
            foreach ($where as $k=>$v){
                if(strlen($v)>0){
                    if($k !== 'time') $wheres[] = [$k,'=',$v];
                }
            }
            if($wheres){
                session($name,$wheres);
                $list = model($this->request->controller())->where($wheres)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->where($wheres)->select()->toArray());
            }else{
                Session::delete($name);
                $list = model($this->request->controller())->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }else{
            if(session($name)){
                $list = model($this->request->controller())->where(session($name))->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->where(session($name))->select()->toArray());
            }else{
                $list = model($this->request->controller())->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        //资产总额
        //进项
        $jmoney = model('assets')->field('money')->where('moneyclass','=',1)->select()->toArray();
        $money = 0;
        foreach ($jmoney as $v){
            $money = $v['money']*1+$money;
        }
        //出项
        $jmoney = model('assets')->field('money')->where('moneyclass','=',2)->select()->toArray();
        $moneys = 0;
        foreach ($jmoney as $v){
            $moneys = $v['money']*1+$moneys;
        }
        //平台总收益
        $money = $money-$moneys;
        $this->assign('ptsy',$money);

        //用户余额
        //进项
        $jmoney = model('wallet')->field('money')->where('status','=',1)->select()->toArray();
        $money = 0;
        foreach ($jmoney as $v){
            $money = $v['money']*1+$money;
        }
        //出项
        $jmoney = model('wallet')->field('money')->where('status','=',2)->select()->toArray();
        $moneys = 0;
        foreach ($jmoney as $v){
            $moneys = $v['money']*1+$moneys;
        }
        $money = $money-$moneys;
        $this->assign('yhye',$money);
        $this->assign('groups',$groups);
        return $this->fetch();
    }
    public function bill()
    {
        $get = input('get.');
        $bill = $get['bill'] ?? 1;
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        if($bill == 1){
            //日汇总
        $list = model('daily')->paginate($limit);
        $page = $list->render();
        $list = $list->toArray()['data'];
        $count = count(model('daily')->select()->toArray());
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);
        }else{
            //月汇总
            $list = model('month')->paginate($limit);
            $page = $list->render();
            $list = $list->toArray()['data'];
            $count = count(model('month')->select()->toArray());
            $this->assign('list',$list);
            $this->assign('page',$page);
            //总个数
            $this->assign('count',$count);
            //显示条数
            $this->assign('limit',$limit);
            //当前页数
            $this->assign('curl',$curl);
            //页码数
            $groups = ceil($count/$limit);
            $this->assign('groups',$groups);
        }
        $this->assign('list',$list);
        $this->assign('bill',$bill);
        return $this->fetch();
    }
    public function billview()
    {
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        $status = $get['status'] ?? 1;
        $bill = $get['bill'] ?? 1;
        $yueid = $get['battery_orderyue'] ?? '';
        $riid = $get['battery_order'] ?? '';
        if(!empty($riid)){
            $this->assign('riid',$riid);
            if($status == 1){
                $user = model('daily')->get($riid)->toArray();
                $list = model('assets')->where([
                    ['create_time','like',$user['time'].'%'],
                    ['moneyclass','=',1]
                ])->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('assets')->where([
                    ['create_time','like',$user['time'].'%'],
                    ['moneyclass','=',1]
                ])->select()->toArray());
            }else{
                $user = model('daily')->get($riid);
                $list = model('assets')->where([
                    ['create_time','like',$user['time'].'%'],
                    ['moneyclass','=',2]
                ])->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('assets')->where([
                    ['create_time','like',$user['time'].'%'],
                    ['moneyclass','=',2]
                ])->select()->toArray());
            }
        }else{
            $this->assign('yueid',$yueid);
            if($status == 1){
                $user = model('month')->get($yueid)->toArray();
                $list = model('assets')->where([
                    ['create_time','like',$user['time'].'%'],
                    ['moneyclass','=',1]
                ])->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('assets')->where([
                    ['create_time','like',$user['time'].'%'],
                    ['moneyclass','=',1]
                ])->select()->toArray());
            }else{
                $user = model('month')->get($yueid)->toArray();
                $list = model('assets')->where([
                    ['create_time','like',$user['time'].'%'],
                    ['moneyclass','=',2]
                ])->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('assets')->where([
                    ['create_time','like',$user['time'].'%'],
                    ['moneyclass','=',2]
                ])->select()->toArray());
            }
        }

        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);


        $this->assign('status',$status);
        $this->assign('bill',$bill);
        return $this->fetch();
    }
    public function dc()
    {
        $get = input('get.');
        $arr = ['A1'=>'日期','B1'=>'支出','C1'=>'收入','D1'=>'来源'];
        $table = 'assets';
        if($get['bill'] == 1)
        {
            $name = '日汇总';
        }else{
            $name = '月汇总';
        }
        $model = model($table)->where('create_time','like',$get['time'] . '%')->select()->toArray();
        $num = 2;
        if($model){
            foreach ($model as $k=>$v){
                if($v['moneyclass'] == 1){
                    $sr = $v['money'];
                    $zc = 0;
                }else{
                    $sr = 0;
                    $zc = $v['money'];
                }
                if($v['class'] == 1){
                    $ly = '购买合伙人';
                }elseif ($v['class'] == 2){
                    $ly = '用户提现';
                }elseif ($v['class'] == 3){
                    $ly = '商品购买';
                }else{
                    $ly = '未知状态';
                }
                $a = [
                    'A'."$num" => $v['create_time'],
                    'B'."$num" => $zc,
                    'C'."$num" => $sr,
                    'D'."$num" => $ly
                ];
                $arr = array_merge($arr,$a);
                $num++;
            }
        }else{
            if($get['time'] == 'undefined'){
                $get['time'] = '没有日期';
            }
                $a = [
                    'A'."$num" => $get['time']?:'没有日期',
                    'B'."$num" => '此时间段没有数据',
                    'C'."$num" => '此时间段没有数据',
                    'D'."$num" => '此时间段没有数据',
                    'E'."$num" => '此时间段没有数据'
                ];
                $arr = array_merge($arr,$a);
        }
        $name = $name. $get['time'] . '.xls';
        $b = exportExcel($arr,$name);
    }

    public function dcs()
    {
        $get = input('get.');
        $table = 'assets';
        $get['status'] = $get['status'] ?? 1;
        if($get['status'] == 1)
        {
            $arr = ['A1'=>'日期','B1'=>'收入','C1'=>'来源'];
            $name = '收入';
            $model = model($table)->where([
                ['create_time','like',$get['time'] . '%'],
                ['moneyclass','=',1]
            ])->select()->toArray();
        }else{
            $arr = ['A1'=>'日期','B1'=>'支出','C1'=>'来源'];
            $name = '支出';
            $model = model($table)->where([
                ['create_time','like',$get['time'] . '%'],
                ['moneyclass','=',2]
            ])->select()->toArray();
        }
        $num = 2;
        if($model){
            foreach ($model as $k=>$v){
                if($v['class'] == 1){
                    $ly = '购买合伙人';
                }elseif ($v['class'] == 2){
                    $ly = '用户提现';
                }elseif ($v['class'] == 3){
                    $ly = '商品购买';
                }else{
                    $ly = '未知状态';
                }
                $a = [
                    'A'."$num" => $v['create_time'],
                    'B'."$num" => $v['money'],
                    'C'."$num" => $ly
                ];
                $arr = array_merge($arr,$a);
                $num++;
            }
        }else{
            if($get['time'] == 'undefined'){
                $get['time'] = '没有日期';
            }
            $a = [
                'A'."$num" => $get['time']?:'没有日期',
                'B'."$num" => '此时间段没有数据',
                'C'."$num" => '此时间段没有数据',
                'D'."$num" => '此时间段没有数据',
                'E'."$num" => '此时间段没有数据'
            ];
            $arr = array_merge($arr,$a);
        }
        $name = $name. $get['time'] . '.xls';
        $b = exportExcel($arr,$name);
        var_dump($b);
    }
    public function view()
    {
        $get = input('get.');
        //1=日，2=月
        if($get['bill'] == 1){
            $user = model('daily')->get($get['sid'])->toArray();
        }else{
            $user = model('month')->get($get['sid'])->toArray();
        }
        $this->assign('bill',$get['bill']);
        $this->assign('time',$user);
        $listru = model('assets')->where([
            ['create_time','like',$user['time'] . '%'],
            ['moneyclass','=','1']
        ])->select()->toArray();
        $listchu = model('assets')->where([
            ['create_time','like',$user['time'] . '%'],
            ['moneyclass','=','2']
        ])->select()->toArray();
        $moneychu = 0;
        foreach ($listchu as $v){
            $moneychu = $moneychu + $v['money'];
        }
        $moneyru = 0;
        foreach ($listru as $v){
            $moneyru = $moneyru + $v['money'];
        }
        $money['ru'] = number_format($moneyru,2);
        $money['chu'] = number_format($moneychu,2);
        $money['heji'] = number_format($moneyru-$moneychu,2);
        $this->assign('hemoney',$money);
        $this->assign('ru',$listru);
        $this->assign('chu',$listchu);
        return $this->fetch();
    }
}
